import React, { Component } from 'react';
import { View, Text, StyleSheet, ScrollView, Image,Alert,Platform,SafeAreaView } from 'react-native';
import { List, Button, Toast, Modal } from 'antd-mobile-rn';
import Utils from '../../utils';
import {NavigationActions,StackActions} from 'react-navigation';
import Jpush from "jpush-react-native";

const Item = List.Item;

export default class User extends Component {

    static navigationOptions = (e) => {
        console.log("wode ::",e)
        return {
            title: "我的",
            tabBarOnPress:(sceen)=>{
                const {defaultHandler,navigation} = sceen;
                console.log("s::",sceen);
                
                Utils.getLoginInfo().then(user=>{
                    navigation.setParams({
                        user:user
                    })
                })

                defaultHandler();
            }
        }
    }

    
    state = {
        user:null
    }

    _dispatch = (navigation,route)=>{
        const resetAction = StackActions.reset({
            index: 0,
            actions: [
                NavigationActions.navigate({routeName:route}),
            ],
        });

       navigation.dispatch(resetAction)
    }



    logOut = ()=>{
        const  {navigate,dispatch} = this.props.screenProps.appNavigation;
        Alert.alert("提示","是否退出账号?",[
            { text: "取消" },
            {
                text: "确定", onPress: () => {
                  Utils.removeStorage("LoginUserInfo").then(()=>{
                    Jpush.deleteAlias(()=>{},()=>{});

                    this._dispatch(this.props.screenProps.appNavigation,"Login");
                    
                  })

                }
            },
        ])
    }

    render() {

        console.log("user::",this.props);

        const {appNavigation} = this.props.screenProps;

        const { getParam,setParams} = this.props.navigation;

        const  user = this.state.user?this.state.user:getParam("user",{});


        return (

            <SafeAreaView style={{ flex: 1 }}>
               

                <ScrollView
                    style={{ flex: 1, backgroundColor: '#f5f5f9' }}
                    automaticallyAdjustContentInsets={false}
                    showsHorizontalScrollIndicator={false}
                    showsVerticalScrollIndicator={false}
                >
                    <View style={{ flexDirection: "row", padding: 20, alignItems: "center",backgroundColor:"#fff",paddingTop:Platform.OS === 'ios'?60:40}}>
                        <Image style={{ width: 60, height: 60, borderRadius: 30 }} source={{ uri:user.image}} />
                        <Text style={{ fontSize: 20, fontWeight: "bold", color: "#000", marginLeft: 15 }}>{user.name?user.name:user.id}</Text>
                    </View>
                    <List>
                        <Item arrow="horizontal" onClick={() => {
                              appNavigation.navigate("UserDetail",{user:user,back:(user)=>{
                                  this.setState({
                                      user:user
                                  })
                              }});
                         }}>
                                个人资料
                        </Item>
                            <Item arrow="horizontal" onClick={() => {
                               appNavigation.navigate("UserTarget");
                             }}>
                                备考目标
                        </Item>
                            <Item arrow="horizontal" onClick={() => {
                                console.log("ssss");
                                appNavigation.navigate("FeedBack");
                             }}>
                                意见反馈
                        </Item>
                    </List>

                    <View>
                        <View style={{marginTop:80}}>
                            <Button onClick={this.logOut.bind(this)} style={{borderRadius:0}} activeStyle={{backgroundColor:"#ccc"}}>退出账号</Button>
                        </View>
                    </View>
                    
                </ScrollView>
            </SafeAreaView>
        )

    }
}